package com.company.hashtable;

/**
 * @author shenhan
 * @create 2020/2/2 21:30
 * @description 链表
 */
public class EmpLinkedList {
    private Emp head;

    //添加雇员到链表
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = emp;
    }

    //遍历
    public void list(int no) {
        if (head == null) {
            System.out.printf("第%d条链表为空\n", no);
            return;
        }
        System.out.printf("第%d条链表信息为", no);
        Emp cur = head;
        while (true) {
            System.out.printf("=>id = %d & name = %s\t", cur.id, cur.name);
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        System.out.println();
    }

    public Emp findEmpById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp cur = head;
        while (true) {
            if (cur.id == id) {
                break;
            }
            if (cur.next == null) {
                cur = null;
                break;
            }
            cur = cur.next;
        }
        return cur;
    }
}
